//
//  ShareView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/3/5.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class ShareView: UIView {
    var titleStr:String = ""
    var descre:String = ""
    var url:String = ""
    var imageUrl:String = "http://source.blistening.cn/bt_source/ttbt/logo/logo.png"
    
    init(frame: CGRect, title:String, des:String, url:String, image:String) {
        super.init(frame: frame)
        self.titleStr = title
        self.descre = des
        self.url = url
        self.imageUrl = image
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
        self.backgroundColor = RGB_Same(0, 0.8)
        
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(closeAction)))
        
        let whiteBack = UIView.init(frame: FRAME_CGFloat(0, SCREEN_HEIGHT - 226, SCREEN_WIDTH, 226))
        whiteBack.backgroundColor = WhiteBackColor
        self.addSubview(whiteBack)
        
        let topLabel = UILabel.init(frame: FRAME_CGFloat(16, 16, 40, 18.5))
        topLabel.font = FONT(13)
        topLabel.textColor = RGB_Same(136)
        topLabel.text = "分享到"
        whiteBack.addSubview(topLabel)
        
        let leftBtn = UIButton(type: UIButton.ButtonType.custom)
        leftBtn.frame = FRAME_CGFloat(SCREEN_WIDTH / 4 - 24, 52, 48, 48)
        leftBtn.setImage(IMAGE(name: "shareWXHY"), for: .normal)
        leftBtn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        leftBtn.tag = 11
        whiteBack.addSubview(leftBtn)
        
        let rightBtn = UIButton(type: UIButton.ButtonType.custom)
        rightBtn.frame = FRAME_CGFloat(SCREEN_WIDTH * 3 / 4 - 24, 52, 48, 48)
        rightBtn.setImage(IMAGE(name: "sharePYQ"), for: .normal)
        rightBtn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        rightBtn.tag = 12
        whiteBack.addSubview(rightBtn)
        
        let leftLabel = UILabel.init(frame: FRAME_CGFloat(0, 107.5, SCREEN_WIDTH / 2, 20))
        leftLabel.font = FONT(14)
        leftLabel.textColor = RGB_Same(53)
        leftLabel.text = "微信好友"
        leftLabel.textAlignment = .center
        whiteBack.addSubview(leftLabel)
        
        let rightLabel = UILabel.init(frame: FRAME_CGFloat(SCREEN_WIDTH / 2, 107.5, SCREEN_WIDTH / 2, 20))
        rightLabel.font = FONT(14)
        rightLabel.textColor = RGB_Same(53)
        rightLabel.text = "朋友圈"
        rightLabel.textAlignment = .center
        whiteBack.addSubview(rightLabel)
        
        let lineView = UIView.init(frame: FRAME_CGFloat(0, 150, SCREEN_WIDTH, 1))
        lineView.backgroundColor = RGB_Same(240, 1)
        whiteBack.addSubview(lineView)
        
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH / 2 - 35, 168, 70, 50)
        button.setTitle("取消分享", for: .normal)
        button.setTitleColor(RGB_Same(53), for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        button.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        button.tag = 13
        whiteBack.addSubview(button)
        
    }
    
    @objc func closeAction() {
        self.removeFromSuperview()
    }
    
    @objc func btnAction(btn:UIButton) {
        switch btn.tag {
        case 11:
            shareAction(type: "0")
        case 12:
            shareAction(type: "1")
        case 13:
            self.removeFromSuperview()
        default:
            self.removeFromSuperview()
        }
    }
    
    func shareAction(type:String) {
        let nData = NSData.init(contentsOf: URLSTR(StringIncludeChineseToString(self.imageUrl)))
        let image = UIImage.init(data: nData! as Data)
        
        let message = WXMediaMessage()
        message.title = self.titleStr
        message.description = self.descre
        message.setThumbImage(image)
        
        let obj = WXWebpageObject()
        obj.webpageUrl = self.url
        message.mediaObject = obj
        
        let req = SendMessageToWXReq()
        req.bText = false
        req.message = message
        switch type {
        case "0":
            req.scene = Int32(WXSceneSession.rawValue)
        case "1":
            req.scene = Int32(WXSceneTimeline.rawValue)
        default:
            self.removeFromSuperview()
            return
        }
        self.removeFromSuperview()
        WXApi.send(req)
    }

}
